import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const RouteView = {
  name: 'RouteView',
  render: h => h('router-view')
}
const routes = [{
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/components/login/Login"),
  },
  {
    path: '/home',
    meta: {
      title: '首页'
    },
    component: () => import('@/components/home/Home'),
    redirect: '/users',
    children: [{
        path: '/users',
        meta: {
          title: '用户列表'
        },
        component: () => import('@/components/home/users/Users')
      },
      {
        path: '/power',
        meta: {
          title: '权限管理'
        },
        children: [

        ]

      },
      {
        path: '/rights',
        meta: {
          title: '权限列表'
        },
        component: () => import('@/components/home/power/rights/Rights')
      },
      {
        path: '/roles',
        meta: {
          title: '角色列表'
        },
        component: () => import('@/components/home/power/roles/role')
      },
      // {
      //   path: '/goods',
      //   meta: {title: '商品列表'},
      //   component: () =>import('@/components/home/goods/list/List')
      // },
      // {
      //   path: '/goods',
      //   meta: {
      //     title: '商品列表'
      //   },
      //   component: () => import('@/components/home/goods/list/List')
      // },
      {
        path: '/goods1',
        meta: {
          title: '商品管理'
        },
        component:RouteView,
        redirect:'/goods',
        children: [{
          path: '/goods',
          meta: {
            title: '商品列表'
          },
          component: () => import('@/components/home/goods/list/List')
        }, 
        {
          path: '/categories',
          meta: {
            title: '商品分类'
          },
          component: () => import('@/components/home/goods/category/Category')
        },
        {
          path: '/params',
          meta: {
            title: '分类参数'
          },
          component: () => import('@/components/home/goods/params/Params')
        }
      ],
        

      },
      {
        path:'/reports1',
        meta:{title:'数据统计'},
        component:RouteView,
        redirect:'/reports',
        children:[
          {
            path:'/reports',
            meta:{title:'数据列表'},
            component:() =>import ('@/components/home/report/Report')
          }
        ]
      }

    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes,
})
router.beforeEach((to, from, next) => {
  // to 将访问哪一个路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行
  //   next() 放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})


export default router